<?php

namespace App\Services\Tools;

use Carbon\Carbon;
use Illuminate\Support\Facades\Log;

class ConstellationService
{
    const CONS_TODAY    = 'today';
    const CONS_TOMORROW = 'tomorrow';
    const CONS_WEEK     = 'week';
    const CONS_MONTH    = 'month';
    const CONS_YEAR     = 'year';

    public static function getData($consName, $type = self::CONS_TODAY)
    {
        $redis_key = 'cons:'.$consName.':'.$type;
        if (app('redis')->exists($redis_key)) {
            $data = app('redis')->get($redis_key);

            return json_decode($data);
        }
        $data = [
            'consName' => $consName,
            'key'      => '',
            'type'     => $type,
        ];
        $url = 'http://web.juhe.cn:8080/constellation/getAll';
        try {
            $result = CurlService::curl_get($url, $data);
            if (isset($result->error_code) && $result->error_code == 0) {
                app('redis')->setex($redis_key, Carbon::now()->diffInSeconds(Carbon::tomorrow()), json_encode($result));

                return $result;
            } else {
                Log::info('获取星座接口数据出错:'.$result);

                return new \ArrayObject();
            }
        } catch (\Exception $e) {
            return new \ArrayObject();
        }
    }

    public static function getConsNameToday()
    {
        $month = Carbon::now()->format('m');
        $day = Carbon::now()->format('d');
        $xingzuo = '';

        if(($month == 1 && $day >= 20) || ($month == 2 && $day <= 18)) {
            $xingzuo = "水瓶座";
        }else if(($month == 2 && $day >= 19) || ($month == 3 && $day <= 20)) {
            $xingzuo = "双鱼座";
        }else if (($month == 3 && $day >= 21) || ($month == 4 && $day <= 19)) {
            $xingzuo = "白羊座";
        }else if (($month == 4 && $day >= 20) || ($month == 5 && $day <= 20)) {
            $xingzuo = "金牛座";
        }else if (($month == 5 && $day >= 21) || ($month == 6 && $day <= 21)) {
            $xingzuo = "双子座";
        }else if (($month == 6 && $day >= 22) || ($month == 7 && $day <= 22)) {
            $xingzuo = "巨蟹座";
        }else if (($month == 7 && $day >= 23) || ($month == 8 && $day <= 22)) {
            $xingzuo = "狮子座";
        }else if (($month == 8 && $day >= 23) || ($month == 9 && $day <= 22)) {
            $xingzuo = "处女座";
        }else if (($month == 9 && $day >= 23) || ($month == 10 && $day <= 23)) {
            $xingzuo = "天秤座";
        }else if (($month == 10 && $day >= 24) || ($month == 11 && $day <= 22)) {
            $xingzuo = "天蝎座";
        }else if (($month == 11 && $day >= 23) || ($month == 12 && $day <= 21)) {
            $xingzuo = "射手座";
        }else if (($month == 12 && $day >= 22) || ($month == 1 && $day <= 19)) {
            $xingzuo = "摩羯座";
        }

        return $xingzuo;
    }

    //获取星座列表
    public static function getConsList()
    {
        return [
            '水瓶座',
            '双鱼座',
            '白羊座',
            '金牛座',
            '双子座',
            '巨蟹座',
            '狮子座',
            '处女座',
            '天秤座',
            '天蝎座',
            '射手座',
            '摩羯座',
        ];
    }

    public static function getDefaultConsellation($user_id)
    {
        $redis_key = 'cons:default:'.$user_id;
        if (app('redis')->exists($redis_key)) {
            return app('redis')->get($redis_key);
        }
        return self::getConsNameToday();
    }

    public static function setDefaultConsellation($user_id, $consName)
    {
        $redis_key = 'cons:default:'.$user_id;
        app('redis')->set($redis_key, $consName);
    }
}
